import { ref, computed } from 'vue';
import { useUserStore } from '@/store/modules/user';
import { getUserStats } from '@/api/user';
import type { UserStats, MenuItem, QuickAction } from '@/types/user';

/**
 * 个人中心业务逻辑
 * 职责：个人中心页面的状态管理和数据处理
 */
export function useUserProfile() {
  const userStore = useUserStore();
  
  // 统计数据
  const stats = ref<UserStats>({
    orderCount: 0,
    messageCount: 0,
    couponCount: 0
  });

  const loading = ref(false);

  // 是否已登录
  const isLogin = computed(() => userStore.hasLogin);

  // 用户信息
  const userInfo = computed(() => {
    if (!userStore.userInfo || !isLogin.value) {
      return {
        avatar: '/static/logo.png',
        name: '未登录',
        phone: ''
      };
    }

    return {
      avatar: userStore.userInfo.avatar || '/static/logo.png',
      name: userStore.userInfo.nickname || '用户',
      phone: userStore.userInfo.phone || ''
    };
  });

  // 加载用户统计数据
  const loadUserStats = async () => {
    if (!userStore.userInfo?.customerId) return;

    try {
      loading.value = true;
      const data = await getUserStats(userStore.userInfo.customerId);
      stats.value = data;
    } catch (error) {
      console.error('加载统计数据失败:', error);
      // 使用默认值
      stats.value = {
        orderCount: 0,
        messageCount: 0,
        couponCount: 0
      };
    } finally {
      loading.value = false;
    }
  };

  // 快捷操作配置
  const quickActions = computed<QuickAction[]>(() => [
    {
      id: 'favorites',
      icon: 'star-filled',
      label: '我的收藏',
      path: '/pages/my/favorites/favorites',
      requireLogin: true
    },
    {
      id: 'address',
      icon: 'location-filled',
      label: '地址管理',
      path: '/pages/my/address/address',
      requireLogin: true
    },
    {
      id: 'evaluation',
      icon: 'chatbubble-filled',
      label: '我的评价',
      path: '/pages/my/evaluation/evaluation',
      requireLogin: true
    },
    {
      id: 'wallet',
      icon: 'wallet-filled',
      label: '我的钱包',
      path: '/pages/my/wallet/wallet',
      requireLogin: true
    }
  ]);

  // 菜单配置
  const menuItems = computed<MenuItem[]>(() => [
    {
      id: 'profile',
      icon: 'person',
      iconColor: '#00C853',
      label: '个人资料',
      path: '/pages/my/selfInfo/selfInfo',
      requireLogin: true
    },
    {
      id: 'wishlist',
      icon: 'list',
      iconColor: '#00C853',
      label: '愿望榜单',
      path: '/pages/my/wishList/wishList',
      requireLogin: true
    },
    {
      id: 'about',
      icon: 'info',
      iconColor: '#00C853',
      label: '关于我们',
      path: '/pages/my/about/about',
      requireLogin: false
    },
    {
      id: 'service',
      icon: 'chatbubble',
      iconColor: '#00C853',
      label: '联系客服',
      path: '/pages/my/customerService/customerService',
      requireLogin: false
    }
  ]);

  // 统一的导航处理（带登录验证）
  const navigateTo = (path: string, requireLogin = true) => {
    if (requireLogin && !isLogin.value) {
      uni.showToast({
        title: '请先登录',
        icon: 'none'
      });
      return;
    }

    uni.navigateTo({ url: path });
  };

  // 处理登录点击
  const handleLoginClick = () => {
    if (!isLogin.value) {
      uni.navigateTo({
        url: '/pages/login/login'
      });
    }
  };

  // 编辑用户信息
  const editUserInfo = () => {
    if (!isLogin.value) {
      uni.showToast({
        title: '请先登录',
        icon: 'none'
      });
      return;
    }

    uni.navigateTo({
      url: '/pages/my/selfInfo/selfInfo'
    });
  };

  return {
    isLogin,
    userInfo,
    stats,
    loading,
    quickActions,
    menuItems,
    loadUserStats,
    navigateTo,
    handleLoginClick,
    editUserInfo
  };
}